public class Exercise1_3_30<Item> {

    private Node node1 = new Node();
    private Node node2 = new Node();
    private Node node3 = new Node();
    private Node node4 = new Node();

    public Exercise1_3_30(){
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node1.item = (Item) "1";
        node2.item = (Item) "2";
        node3.item = (Item) "3";
        node4.item = (Item) "4";
        Node node_1 = reverse_1(node1);
        System.out.println(node_1.item+" ");
        Node node_2 = reverse_2(node_1);
        System.out.println(node_2.item+" ");
    }

    private class Node{
        Item item;
        Node next;
    }
    public Node reverse_1(Node x){
        // 迭代方式
        Node first = x;
        Node reverse = null;

        while (first != null){
            Node second = first.next;
            first.next = reverse;
            reverse = first;
            first = second;
        }
        return reverse;
    }

    public Node reverse_2(Node first){
        // 递归方式
        if (first == null)
            return null;
        if (first.next == null)
            return first;
        Node second = first.next;
        Node rest = reverse_2(second);
        second.next = first;
        first.next = null;
        return rest;
    }

    public static void main(String args[]){
        Exercise1_3_30 test = new Exercise1_3_30();
    }
}
